﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GSManager
{
    public partial class ThemPhieuNo : Form
    {
        private PhieuNo phieuno;
        private KhachHang khachhang;
        private bool tra;
        public ThemPhieuNo()
        {
            InitializeComponent();
        }

        public ThemPhieuNo(bool _tra,string sophieuban,KhachHang _khachhang,DateTime ngayno,decimal tienno,decimal tientra,string nguoilapphieu )
        {
            InitializeComponent();
            phieuno=new PhieuNo();
            textBox_SoPhieuBan.Text=sophieuban;
            textBox_SoPhieuNo.Text = (phieuno.DemSoPhieuNo(int.Parse(sophieuban))+1).ToString();
            khachhang = _khachhang;
            textBox_TenKH.Text = khachhang.TenKH;
            textBox_DiaChi.Text = khachhang.DiaChi;
            dateTimePicker_NgayNo.Value = ngayno;
            textBox_TienConNo.Text = String.Format("{0:0,0}", tienno);
            textBox_SoTienTra.Text=String.Format("{0:0,0}", tientra);
            textBox_DuNo.Text = String.Format("{0:0,0}", tienno-tientra);
            phieuno.NguoiLapPhieu = nguoilapphieu;
            button_Thoat.Enabled = false;
            tra = _tra;
            this.Refresh();
        }
        private void button_DongY_Click(object sender, EventArgs e)
        {
            if (dateTimePicker_NgayTra.Value < dateTimePicker_NgayNo.Value)
            {
                MessageBox.Show("Ngày trả không được trước ngày nợ!");
                dateTimePicker_NgayTra.Focus();
                return;
            }
            decimal sotientra = decimal.Parse(textBox_SoTienTra.Text);
            decimal sotienno = decimal.Parse(textBox_TienConNo.Text);
            
            if (sotienno < sotientra)
            {
                MessageBox.Show("Số tiền trả không thể lớn hơn số tiền nợ!");
                textBox_SoTienTra.Focus();
                return;
            }
            phieuno.SoPhieuBan = int.Parse(textBox_SoPhieuBan.Text);
            phieuno.SoPhieuNo = int.Parse(textBox_SoPhieuNo.Text);
            phieuno.MaKhachHang = khachhang.MaKH;
            phieuno.NgayNo = dateTimePicker_NgayNo.Value;
            phieuno.NgayTra = dateTimePicker_NgayTra.Value;
            phieuno.SoTienTra = sotientra;
            phieuno.NoDu = sotienno - sotientra;
            phieuno.ThemPhieuNo();
            if (tra == false) khachhang.TienNo += phieuno.NoDu;
            else khachhang.TienNo -= sotientra;
            khachhang.CapNhatTienNo();
            button_DongY.Enabled = false;
            button_Thoat.Enabled = true;
        }

        private void button_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button_InHD_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(0, phieuno.SoPhieuBan);
            cuaso.ShowDialog();
        }

        private void textBox_SoTienTra_Leave(object sender, EventArgs e)
        {
            decimal sotientra = decimal.Parse(textBox_SoTienTra.Text);
            decimal sotienno = decimal.Parse(textBox_TienConNo.Text);
            textBox_DuNo.Text = String.Format("{0:0,0}", sotienno - sotientra);
        }
    }
}
